//
//  for_each.cpp
//  learnC++
//
//  Created by apple on 2018/5/30.
//  Copyright © 2018年 yunfan.com. All rights reserved.
//

#include "for_each.hpp"

using namespace std;

void test_for_each(){

    int arr[] = {10,23,45,768,98,90,12};

    //int 改为auto比较合适
    for(/*const*/ auto  &element : arr){
        cout << ++element << endl;
    }
    
    std::vector<int> arr2 = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
    for(const auto &element : arr2){
        cout << element << endl;
    }
    
    for(auto &element : arr2){
        element += 1;
    }
    
    std::string names[] = {"jack","rose","lili"};
    
    std::string name;
    name = "kkk";
    name = "lll";
    
    name.assign("jjjj");
    name.erase();//擦除
    
    
    for(auto &element : names){
        if (element == "jack") {
            cout << "jack\n";
            continue;
        }
        element = "...";
        
        element.assign("PPPP");
        cout << "呵呵\n";
        
        if (element.find("x") == std::string::npos) {
            //未找到
        }
        
        //[] 和at功能一样，at提供边界检查
        char & l = element[2];
        char & k = element.at(2);
        
        //back 返回最后一个字符的引用
        element.back() = 'o';
        element.push_back('k');
        
        
    }

    
    std::vector<const char *> arr3 = {"jack","rose","lili"};
    
    for(const char * &element : arr3){
        if (!strcmp(element, "jack")) {
            cout << "jack\n";
            break;
        }
        
        cout << element << "呵呵\n";
    }
    
    
    
}
